home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Think Class Libraries / WASTE TCL 2.0b2 / WASTEEdit / CEditApp.cp next >
Encoding:
Text File  |  1996-06-16  |  8.5 KB  |  361 lines  |  [TEXT/KAHL]

  1. /******************************************************************************
  2.     CEditApp.c
  3.     
  4.     Application methods for a tiny editor.
  5.         
  6.     Copyright © 1989 Symantec Corporation. All rights reserved.
  7.  
  8.  ******************************************************************************/
  9.  
  10. #include "Commands.h"
  11. #include "CBartender.h"
  12. #include "CEditApp.h"
  13. #include "CEditDoc.h"
  14. #include "Global.h"
  15. #include "WASTE.h"
  16. #ifndef __SCRIPT__
  17. #include <Script.h>
  18. #endif
  19. #include <TextServices.h>
  20. #include <GestaltEqu.h>
  21. #include <FragLoad.h>
  22. #include <Drag.h>
  23.  
  24. #include "CTSMSwitchboard.h"
  25. #include "CTSMDesktop.h"
  26. #include "CWASTEClipboard.h"
  27.  
  28. #if WASTE_OBJECT_ARCHIVE
  29. #include "WASTE_Objects.h"
  30. #endif
  31.  
  32. static Boolean    TSMAvailable(void);
  33. static Boolean    DragAndDropAvailable(void);
  34.  
  35. short gUsingTSM = false;
  36. Boolean gHasDragAndDrop = false;
  37.  
  38. extern    OSType        gSignature;
  39. extern    CBartender    *gBartender;
  40. extern    CDesktop     *gDesktop;
  41. extern CClipboard    *gClipboard;
  42.  
  43. #define        MENUcolor            135
  44.  
  45. #define        kExtraMasters        10
  46. #define        kRainyDayFund        45000
  47. #define        kCriticalBalance    40000
  48. #define        kToolboxBalance        20000
  49.  
  50.  
  51. /***
  52.  * IEditApp
  53.  *
  54.  *    Initialize the application. Your initialization method should
  55.  *    at least call the inherited method. If your application class
  56.  *    defines its own instance variables or global variables, this
  57.  *    is a good place to initialize them.
  58.  *
  59.  ***/
  60.  
  61. void CEditApp::IEditApp(void)
  62.  
  63. {
  64.         // stuff added for WASTE-TCL
  65.     gUsingTSM = TSMAvailable() && (InitTSMAwareApplication()==noErr);
  66.     gHasDragAndDrop = DragAndDropAvailable();
  67.  
  68.     CApplication::IApplication( kExtraMasters, kRainyDayFund,
  69.                         kCriticalBalance, kToolboxBalance);
  70. #if WASTE_OBJECT_ARCHIVE
  71.     InitWASTEObjectHandlers();
  72. #endif
  73.  
  74. }
  75.  
  76.  
  77.  
  78. /***
  79.  * SetUpFileParameters
  80.  *
  81.  *    In this routine, you specify the kinds of files your
  82.  *    application opens.
  83.  *
  84.  *
  85.  ***/
  86.  
  87. void CEditApp::SetUpFileParameters(void)
  88.  
  89. {
  90.     inherited::SetUpFileParameters();    /* Be sure to call the default method */
  91.  
  92.         /**
  93.          **    sfNumTypes is the number of file types
  94.          **    your application knows about.
  95.          **    sfFileTypes[] is an array of file types.
  96.          **    You can define up to 4 file types in
  97.          **    sfFileTypes[].
  98.          **
  99.          **/
  100.  
  101.     sfNumTypes = 1;
  102.     sfFileTypes[0] = 'TEXT';
  103.  
  104.         /**
  105.          **    Although it's not an instance variable,
  106.          **    this method is a good place to set the
  107.          **    gSignature global variable. Set this global
  108.          **    to your application's signature. You'll use it
  109.          **    to create a file (see CFile::CreateNew()).
  110.          **
  111.          **/
  112.  
  113.     gSignature = '???\?';
  114. }
  115.  
  116.  
  117. /***
  118.  * SetUpMenus
  119.  *
  120.  *    In this method, you add special menu items and set the
  121.  *    menu item dimming and checking options for your menus.
  122.  *    The most common special menu items are the names of the
  123.  *    fonts. For this tiny editor, you also want to set up the
  124.  *    dimming and checking options so only the current font
  125.  *    and size are checked.
  126.  *
  127.  ***/
  128.  
  129. void CEditApp::SetUpMenus(void)
  130.  
  131. {
  132.         /**
  133.          ** Let the default method read the menus from
  134.          **    the MBAR 1 resource.
  135.          **
  136.          **/
  137.  
  138.     inherited::SetUpMenus();
  139.  
  140.         /**
  141.          ** Add the fonts in the  system to the
  142.          **    Font menu. Remember, MENUfont is one
  143.          **    of the reserved font numbers.
  144.          **
  145.          **/
  146.  
  147.     AddResMenu(GetMHandle(MENUfont), 'FONT');
  148.  
  149.         /**
  150.          **    The UpdateMenus() method sets up the dimming
  151.          **    for menu items. By default, the bartender dims
  152.          **    all the menus, and each bureaucrat is reponsible
  153.          **    for turning on the items that correspond to the commands
  154.          **    it can handle.
  155.          **
  156.          **    Set up the options here. The edit pane's UpdateMenus()
  157.          **    method takes care of doing the work.
  158.          **
  159.          **    For Font and Size menus, you want all the items to
  160.          **    be enabled all the time. In other words, you don't
  161.          **    want the bartender to ever dim any of the items
  162.          **    in these two menus.
  163.          **
  164.          **/
  165.  
  166.     gBartender->SetDimOption(MENUfont, dimNONE);
  167.     gBartender->SetDimOption(MENUsize, dimNONE);
  168.     gBartender->SetDimOption(MENUstyle, dimNONE);
  169.     gBartender->SetDimOption(MENUcolor, dimNONE);
  170.  
  171.         /**
  172.          **    For Font and Size menus, one of the items
  173.          **    is always checked. Setting the unchecking option
  174.          **    to TRUE lets the bartender know that it should
  175.          **    uncheck all the menu items because an UpdateMenus()
  176.          **    method will check the right items.
  177.          **    For the Style menu, uncheck all the items and
  178.          **    let the edit pane's UpdateMenus() method check the
  179.          **    appropriate ones.
  180.          **
  181.          **/
  182.  
  183.     gBartender->SetUnchecking(MENUfont, TRUE);
  184.     gBartender->SetUnchecking(MENUsize, TRUE);
  185.     gBartender->SetUnchecking(MENUstyle, TRUE);
  186.     gBartender->SetUnchecking(MENUcolor, TRUE);
  187. }
  188.  
  189.  
  190.  
  191. /***
  192.  * CreateDocument
  193.  *
  194.  *    The user chose New from the File menu.
  195.  *    In this method, you need to create a document and send it
  196.  *    a NewFile() message.
  197.  *
  198.  ***/
  199.  
  200. void CEditApp::CreateDocument()
  201.  
  202. {
  203.     CEditDoc    *theDocument = NULL;
  204.     
  205.     // In the event that creating the document fails,
  206.     // we setup an exception handler here. If any
  207.     // of the methods called within the scope of this
  208.     // TRY block fail, an exception will be raised and
  209.     // control will be transferred to the CATCH block.
  210.     // Here, the CATCH block takes care of disposing
  211.     // of the partially created document.
  212.     
  213.     TRY
  214.     {
  215.         theDocument = new(CEditDoc);
  216.             
  217.             /**
  218.              **    Send your document an initialization
  219.              **    message. The first argument is the
  220.              **    supervisor (the application). The second
  221.              **    argument is TRUE if the document is printable.
  222.              **
  223.              **/
  224.         
  225.         theDocument->IEditDoc(this, TRUE);
  226.     
  227.             /**
  228.              **    Send the document a NewFile() message.
  229.              **    The document will open a window, and
  230.              **    set up the heart of the application.
  231.              **
  232.              **/
  233.         theDocument->NewFile();
  234.     }
  235.     CATCH
  236.     {
  237.         ForgetObject( theDocument);
  238.     }
  239.     ENDTRY;
  240. }
  241.  
  242. /***
  243.  * OpenDocument
  244.  *
  245.  *    The user chose Open… from the File menu.
  246.  *    In this method you need to create a document
  247.  *    and send it an OpenFile() message.
  248.  *
  249.  *    The macSFReply is a good SFReply record that contains
  250.  *    the name and vRefNum of the file the user chose to
  251.  *    open.
  252.  *
  253.  ***/
  254.  
  255. void CEditApp::OpenDocument(SFReply *macSFReply)
  256.  
  257. {
  258.     CEditDoc    *theDocument = NULL;
  259.  
  260.     // In the event that opening the document fails,
  261.     // we setup an exception handler here. If any
  262.     // of the methods called within the scope of this
  263.     // TRY block fail, an exception will be raised and
  264.     // control will be transferred to the CATCH block.
  265.     // Here, the CATCH block takes care of disposing
  266.     // of the partially opened document.
  267.  
  268.     TRY
  269.     {    
  270.         theDocument = new(CEditDoc);
  271.             
  272.             /**
  273.              **    Send your document an initialization
  274.              **    message. The first argument is the
  275.              **    supervisor (the application). The second
  276.              **    argument is TRUE if the document is printable.
  277.              **
  278.              **/
  279.         
  280.         theDocument->IEditDoc(this, TRUE);
  281.     
  282.             /**
  283.              **    Send the document an OpenFile() message.
  284.              **    The document will open a window, open
  285.              **    the file specified in the macSFReply record,
  286.              **    and display it in its window.
  287.              **
  288.              **/
  289.         theDocument->OpenFile(macSFReply);
  290.     }
  291.     CATCH
  292.     {
  293.         ForgetObject( theDocument);
  294.     }
  295.     ENDTRY;
  296. }
  297.  
  298. /*** Destructor added for WASTE support ***/
  299. CEditApp::~CEditApp()
  300. {
  301.     if (gUsingTSM) CloseTSMAwareApplication();
  302. #if WASTE_OBJECT_ARCHIVE
  303.     ExitWASTEObjectHandlers();
  304. #endif
  305. }
  306.  
  307. // make TSMSwitchboard
  308. void CEditApp::MakeSwitchboard(void)
  309. {
  310.     if (gSystem.hasAppleEvents && gUsingTSM)
  311.     {
  312.         FailOSErr(WEInstallTSMHandlers());
  313.     }
  314.     itsSwitchboard = (CSwitchboard *)new CTSMSwitchboard();
  315.     itsSwitchboard->InitAppleEvents(); // needed for TCL 2.0.3
  316. }
  317.  
  318. // make TSMDesktop
  319. void CEditApp::MakeDesktop(void)
  320. {
  321.     gDesktop = (CDesktop *)new CTSMDesktop();
  322. }
  323.  
  324. /******************************************************************************
  325.  TSMAvailable - returns true if the TextService Manager is available
  326.  ******************************************************************************/
  327.  
  328. static Boolean TSMAvailable(void)
  329. {
  330.     long    response;
  331.  
  332.     return (Gestalt(gestaltTSMgrVersion, &response)==noErr);
  333. }
  334.  
  335. /******************************************************************************
  336.  DragAndDropAvailable - returns true if drag and drop is available
  337.  ******************************************************************************/
  338.  
  339. static Boolean DragAndDropAvailable(void)
  340. {
  341.     long response;
  342.  
  343.     if (Gestalt(gestaltDragMgrAttr, &response) != noErr) return false;
  344.     if ((response & (1 << gestaltDragMgrPresent)) == 0) return false;
  345.     if ((Ptr)InstallTrackingHandler == (Ptr)kUnresolvedSymbolAddress) return false;
  346.     return true;
  347. }
  348.  
  349. /******************************************************************************
  350.  MakeClipboard
  351.  
  352.         Create the global Clipboard object
  353.  ******************************************************************************/
  354.  
  355. void    CEditApp::MakeClipboard(void)
  356. {
  357.     CWASTEClipboard    *wasteClip = TCL_NEW(CWASTEClipboard,(true));
  358.  
  359.     gClipboard = wasteClip;
  360. }
  361.